package cn.sirun.com.fca.account.view.impl;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.jungly.gridpasswordview.GridPasswordView;
import com.tencent.cloud.iov.common.flow.view.impl.BaseLoadingFlowView;
import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.kernel.constant.StatusCode;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.router.Router;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.cloud.iov.util.SRPUtils;
import com.tencent.cloud.iov.util.crypt.CustomSRPClient;
import com.tencent.cloud.uikit.utils.SystemUiUtils;

import cn.sirun.com.fca.account.R;
import cn.sirun.com.fca.account.constant.AccountActivityConst;
import cn.sirun.com.fca.account.constant.AccountIntentConst;
import cn.sirun.com.fca.account.constant.CaptchaConst;
import cn.sirun.com.fca.account.model.bind.PinSetParamResponse;
import cn.sirun.com.fca.account.model.bind.PinSetParamsRequest;
import cn.sirun.com.fca.account.model.bind.VerifyPinRequest;
import cn.sirun.com.fca.account.presenter.IInputOldPinPresenter;
import cn.sirun.com.fca.account.presenter.impl.InputOldPinPresenter;
import cn.sirun.com.fca.account.uitl.AccountStatusUtils;
import cn.sirun.com.fca.account.view.IInputOldPinView;

public class InputOldPinFlowView extends BaseLoadingFlowView<IInputOldPinPresenter>
        implements IInputOldPinView {
    private GridPasswordView mPinGPV;
    private TextView mSureTV, mForgetPinTV;

    private CustomSRPClient mCustomSRPClient;

    private String oldPin = "";


    public InputOldPinFlowView(Activity activity) {
        super(activity);
    }

    public InputOldPinFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setUpActionBar();
        initView();

        mCustomSRPClient = SRPUtils.getCustomSRPClient();

    }

    private void setUpActionBar() {
        final Activity activity = getActivity();
        SystemUiUtils.showActionBar(activity, "");
    }

    @Override
    protected IInputOldPinPresenter createPresenter() {
        return new InputOldPinPresenter();
    }

    private void initView() {
        setContentView(R.layout.input_old_pin_activity);
        final Activity activity = getActivity();
        mPinGPV = activity.findViewById(R.id.gpv_pin);
        mSureTV = activity.findViewById(R.id.tv_sure);
        mForgetPinTV = activity.findViewById(R.id.tv_forget_pin);

        mPinGPV.togglePasswordVisibility();

        mPinGPV.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public void onTextChanged(String psw) {
                if (psw.length() == CaptchaConst.PIN_LENGTH) {
                    mSureTV.setBackgroundResource(R.drawable.s_btn_bg_border);
                    mSureTV.setEnabled(true);
                } else {
                    mSureTV.setBackgroundResource(R.drawable.s_btn_default_bg_border);
                    mSureTV.setEnabled(false);
                }
            }

            @Override
            public void onInputFinish(String psw) {
                oldPin = psw;
            }
        });

        mForgetPinTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = Router.fromPath(AccountActivityConst.Path.PERSONAL_CAPTCHA);
                intent.putExtra(AccountIntentConst.FROM, AccountIntentConst.FORGET_ACCOUNT_PIN);
                ActivityUtils.startActivityForResult(activity, intent);
            }
        });

        mSureTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PinSetParamsRequest request = new PinSetParamsRequest();

                getPresenter().getUpdatePinSetParams(request);

            }
        });
    }

    @Override
    public void getUpdatePinSetParamsResult(BaseResponse<PinSetParamResponse> response) {
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            /**
             * 获取修改pin参数成功后，进行验证pin
             */

            String salt = response.getData().getSalt();
            String group = response.getData().getGroup();
            String publicKey = response.getData().getB();

            String[] authenticateInfo = SRPUtils.generateAuthenticateInfo(mCustomSRPClient,
                    oldPin, salt, group, publicKey);

            VerifyPinRequest request = new VerifyPinRequest(authenticateInfo[0],
                    authenticateInfo[1]);

            getPresenter().verifyPin(request);
        }
    }

    @Override
    public void verifyPinResult(BaseResponse<PinSetParamResponse> response) {
        int code = response.getCode();

        if (code == StatusCode.SUCCESS) {
            Intent intent = Router.fromPath(AccountActivityConst.Path.SETTING_PIN);
            ActivityUtils.startActivityForResult(getActivity(), intent);

        }else{
            AccountStatusUtils.showToast(getActivity(), 0, R.string.pin_error);
        }
    }
}
